package com.treetao.union.core.util.system;

import java.lang.management.ManagementFactory;
import java.util.Properties;

import com.sun.management.OperatingSystemMXBean;

public final class SystemInfoUtil {
	public static Properties props = System.getProperties();// 获取当前的系统属性
	private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
	
	public static void main(String[] args) {
		SystemInfo info = getAll();
		System.out.println(info.toString());
	}

	/**
	 * 获取所有参数
	 * @return
	 */
	public static SystemInfo getAll() {
		SystemInfo info = new SystemInfo();
		info.setFreeMemorySize(getFreeMemorySize());
		info.setJavaClassVersion(getJavaClassVersion());
		info.setJavaVersion(getJavaVersion());
		info.setOsArch(getOsArch());
		info.setOsName(getOsName());
		info.setOsVersion(getOsVersion());
		info.setPercentUsedMemory(getUsedMemoryPercent());
		info.setTotalMemorySize(getTotalMemorySize());
		return info;
	}
	
	/**
	 * 总内存大小
	 * @return 单位MB
	 */
	public static double getTotalMemorySize() {
		return osmxb.getTotalPhysicalMemorySize()/1024/1024;
	}
	
	/**
	 * 空闲内存大小
	 * @return 单位MB
	 */
	public static double getFreeMemorySize() {
		return osmxb.getFreePhysicalMemorySize()/1024/1024;
	}
	
	/**
	 * 内存已使用百分比
	 * @return 50 ，即50%
	 */
	public static int getUsedMemoryPercent() {
		double totalvirtualMemory = osmxb.getTotalPhysicalMemorySize();
		double freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();

		double value = freePhysicalMemorySize / totalvirtualMemory;
		int percentMemoryLoad = (int) ((1 - value) * 100);
		return percentMemoryLoad;
	}

	/**
	 * 操作系统名称
	 * 
	 * @return
	 */
	public static String getOsName() {
		return props.getProperty("os.name");
	}

	/**
	 * 操作系统版本
	 * 
	 * @return
	 */
	public static String getOsVersion() {
		return props.getProperty("os.version");
	}

	/**
	 * 操作系统架构
	 * 
	 * @return
	 */
	public static String getOsArch() {
		return props.getProperty("os.arch");
	}

	/**
	 * Java运行版本
	 * 
	 * @return
	 */
	public static String getJavaVersion() {
		return props.getProperty("java.version");
	}

	/**
	 * Java类格式版本
	 * 
	 * @return
	 */
	public static String getJavaClassVersion() {
		return props.getProperty("java.class.version");
	}

}
